<template>
  <div class="carouselImageGroupComponent" v-if="item">
    <draggable
      class="vlonImgGroupWrapper"
      v-model="carouselList"
      handle=".el-icon-rank"
      v-bind="imageSwiperOptions"
      @end="listImageSwiperOptionsChange"
      v-if="carouselList && carouselList.length > 0"
    >
      <transition-group tag="ul" class="list">
        <li
          class="vlonImgGroupItem"
          v-for="(cl, clIndex) in carouselList"
          :key="cl.id"
          :data-id="cl.id"
        >
          <i class="el-icon-rank"></i>

          <div class="imgWrapper">
            <div
              class="uploadBtn"
              v-if="!cl.imgUrl"
              @click="addChooseImg(clIndex)"
            >
              <i class="el-icon-plus"></i>
              <span class="txt">添加图片</span>
            </div>
            <img v-if="cl.imgUrl != ''" :src="cl.imgUrl" alt="" />
            <span
              v-if="cl.imgUrl != ''"
              class="repaceImg"
              @click="updateChooseImg(clIndex)"
              >替换
            </span>
            <span
              v-if="cl.imgUrl != ''"
              class="el-icon-error delImg"
              @click="delImg(clIndex)"
            ></span>
          </div>
          <div class="chooseUrl">
            <div class="urlBox">
              <div class="haveUrl" v-if="cl.linkName || cl.linkUrl">
                <el-button plain disabled class="haveUrlBtn">
                  <i class="el-icon-paperclip"></i>
                  <span>{{ cl.linkName || cl.linkUrl || "" }}</span>
                </el-button>
                <el-button
                  type="text"
                  class="updateHref"
                  @click="updateLinkHref(clIndex)"
                  >修改</el-button
                >
              </div>
              <div class="noUrl" v-else>
                <el-button plain class="noUrlBtn" @click="addLinkHref(clIndex)">
                  <i class="el-icon-paperclip"></i>
                  选择链接
                </el-button>
              </div>
            </div>
            <p class="tips">建议图片宽度750px，高度跟随第一张图片的高度变化</p>
          </div>
          <span
            class="el-icon-error delImageSwiperItem"
            v-if="carouselList.length > 1"
            @click="delImgItem(cl.id)"
          ></span>
        </li>
      </transition-group>
    </draggable>

    <el-button
      style="width: 100%"
      size="small"
      @click="listImageSwiperAddImageSwiperItem"
      >+添加图片</el-button
    >
  </div>
</template>

<script>
import { mapState } from "vuex";
import Bus from "@/utils/bus";
import bannerIcon from "@/assets/images/decorate/bannerIcon.png";
import draggable from "vuedraggable";
export default {
  name: "carouselImageGroupComponent",
  data() {
    return {
      carouselList: null,
      currentIndex: 0,
      bannerIcon,
      imageSwiperOptions: {
        group: { name: "layout", pull: false, put: false },
        draggable: ".vlonImgGroupItem",
        forceFallback: true,
        sort: true,
        animation: 300,
      },
    };
  },
  props: ["item", "rightIndex", "catalogIndex", "id"],
  components: { draggable },
  created() {},
  mounted() {
    this.$nextTick(() => {
      this.init();
    });
    //选择图片回调
    Bus.$off(`chooseCarouseImgGroupAfter${this.id}`);
    Bus.$on(`chooseCarouseImgGroupAfter${this.id}`, (params) => {
      this.carouselList[this.currentIndex].imgUrl = params.url;
      this.updateData();
    });
    //选择跳转链接回调
    Bus.$off(`carouseImgChooseLinkGroupAfter${this.id}`);
    Bus.$on(`carouseImgChooseLinkGroupAfter${this.id}`, (params) => {
      this.carouselList[this.currentIndex].linkUrl = params.url;
      this.carouselList[this.currentIndex].linkName = params.name;
      //此处只需要汇总到vuex集合，不需要更新main中组件
      this.updateData({
        needUpdateMainChild: false,
      });
    });
  },
  methods: {
    init() {
      if (this.item) {
        this.carouselList = null;
        this.$nextTick(() => {
          let oItem = JSON.parse(JSON.stringify(this.item));
          this.carouselList = JSON.parse(JSON.stringify(oItem.list));
        });
      }
    },
    //同时更新主视图区域中的数据
    updateData(params) {
      //标识是否不需要更新Main中的组件
      let needUpdateMainChild = true;
      // if (this.item.ename === "pageName") {
      //     needUpdateMain = false;
      // }
      if (params !== undefined && params !== null) {
        needUpdateMainChild = params.needUpdateMainChild;
      }
      //更新给直属父级
      this.$emit(
        "updateParent",
        JSON.stringify({
          value: JSON.stringify(this.carouselList),
          ename: this.item.ename,
          rightIndex: this.rightIndex,
          catalogIndex: this.catalogIndex,
          needUpdateMainChild: needUpdateMainChild,
          needUpdateMain:false
        })
      );
    },
    //右侧 轮播图组件 拖拽 改变顺序 后触发的事件
    listImageSwiperOptionsChange() {
      //同时改变主视图区域中的数据
      const carouselList = JSON.stringify(this.carouselList);
      this.carouselList = null;
      this.carouselList = JSON.parse(carouselList);
      this.$nextTick(() => {
        this.updateData();
      });
    },
    //在右侧，为轮播图组件 (点击) 选择 图片
    addChooseImg(index) {
      this.currentIndex = index;
      Bus.$emit("openChooseImgGroup", {
        ename: "carouseImgGroup",
        controlid: this.id,
      });
    },
    //在右侧，为轮播图组件 (点击) 替换选择 图片
    updateChooseImg(index) {
      this.currentIndex = index;
      Bus.$emit("openChooseImgGroup", {
        ename: "carouseImgGroup",
        controlid: this.id,
        imgUrl: this.carouselList[index].imgUrl,
      });
    },

    //在右侧轮播图组件中，点击删除图片，回到待上传图片的场景
    delImg(index) {
      this.carouselList[index].imgUrl = "";
      //同时更新主视图区域中的数据
      this.updateData();
    },
    //在右侧，点击修改图片的跳转链接
    updateLinkHref(index) {
      this.currentIndex = index;
      Bus.$emit("openChooseLinkGroup", {
        name: "carouseImgChooseLinkGroup",
        controlid: this.id,
        linkUrl: this.carouselList[index].linkUrl,
        linkName: this.carouselList[index].linkName,
      });
      //打开后回写弹窗中选中状态，现在没有唯一id无法定位到确定项,所以暂时搁置-----------
    },
    //在右侧，点击增加图片的跳转链接
    addLinkHref(index) {
      this.currentIndex = index;
      Bus.$emit("openChooseLinkGroup", {
        name: "carouseImgChooseLinkGroup",
        controlid: this.id,
      });
      //打开后初始化弹窗中选中状态，现在没有唯一id无法定位到确定项,所以暂时搁置-----------
    },
    //点击 为轮播图组件  内容模块 删除 图片
    delImgItem(id) {
      this.carouselList = this.carouselList.filter((row) => row.id != id);
      //同时更新主视图区域中的数据
      this.updateData();
    },
    //点击 为轮播图组件  内容模块 追加 图片
    listImageSwiperAddImageSwiperItem() {
      this.carouselList.push({
        id: "vlonImgGroupItemId_" + new Date().getTime(),
        height: 150,
        imgUrl: "",
        default: this.bannerIcon,
        linkUrl: "",
        linkName: "",
      });
      //同时更新主视图区域中的数据
      this.updateData();
    },
  },
  computed: mapState(["decorate"]),
  watch: {},
};
</script>

<style scoped lang="scss">
.carouselImageGroupComponent {
  .vlonImgGroupItem {
    display: flex;
    position: relative;
    background: #f4f6f8;
    border-radius: 2px;
    box-sizing: border-box;
    border: 1px solid #e9edef;
    margin-bottom: 10px;
    align-items: center;
    padding: 10px 10px;
    .delImageSwiperItem {
      cursor: pointer;
      &:hover {
        cursor: pointer;
      }
    }
    & > i {
      margin-right: 10px;
    }
    .imgWrapper {
      position: relative;
      display: flex;
      align-items: center;
      height: 60px;
      background-color: #fff;
      margin-right: 10px;
      width: 60px;
      .uploadBtn {
        width: 100%;
        height: 100%;
        display: flex;
        box-sizing: border-box;
        border-radius: 2px;
        justify-content: center;
        align-items: center;
        background-color: #fff;
        flex-direction: column;
        position: relative;
        cursor: pointer;
        border: 1px solid #e9edef;
        i {
          color: #0079fe;
          font-weight: bold;
          margin-top: 5px;
          margin-bottom: 5px;
        }
        span.txt {
          font-size: 12px;
          color: #b8b9bd;
        }
        .uploadImgInp {
          position: absolute;
          top: 0;
          left: 0;
          z-index: 2;
          width: 100%;
          height: 100%;
          background-color: transparent;
          opacity: 0;
          font-size: 0px;
          cursor: pointer;
          &:hover {
            cursor: pointer;
          }
        }
      }
      img {
        width: 100%;
        height: 100%;
        display: flex;
      }
      .repaceImg,
      .delImg {
        position: absolute;
        z-index: 3;
        opacity: 0.5;
        display: none;
        cursor: pointer;
        &:hover {
          cursor: pointer;
        }
      }
      .repaceImg {
        left: 0;
        bottom: 0px;
        width: 100%;
        height: 20px;
        font-size: 12px;
        background-color: rgba(0, 0, 0, 0.8);
        color: #fff;
        justify-content: center;
        .uploadImgInp {
          position: absolute;
          top: 0;
          left: 0;
          z-index: 2;
          width: 100%;
          height: 100%;
          background-color: transparent;
          opacity: 0;
          font-size: 0px;
          cursor: pointer;
          &:hover {
            cursor: pointer;
          }
        }
      }
      .delImg {
        right: -4px;
        top: -4px;
      }
      &:hover {
        .repaceImg,
        .delImg {
          display: flex;
        }
      }
    }
    .chooseUrl {
      display: flex;
      flex: 1;
      flex-direction: column;
      .urlBox {
        .haveUrl {
          position: relative;
          .haveUrlBtn {
            padding: 0;
            height: 32px;
            width: 100%;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            padding-left: 30px;
            box-sizing: border-box;
            position: relative;
            i {
              border-right: 1px solid transparent;
              height: 32px;
              display: flex;
              box-sizing: border-box;
              align-items: center;
              justify-content: center;
              position: absolute;
              width: 30px;
              top: 0;
              left: 0;
              z-index: 2;
            }
            &:hover {
              i {
                border-color: #eee;
              }
            }
          }

          .updateHref {
            padding: 0;
            position: absolute;
            top: 0;
            right: 0;
            z-index: 2;
            height: 32px;
            display: flex;
            align-items: center;
            width: 60px;
            justify-content: center;
            border-radius: 0;
            border-left: 1px solid #eee;
          }
        }
        .noUrl {
          .noUrlBtn {
            padding: 0;
            height: 32px;
            width: 100%;
            display: flex;
            justify-content: flex-start;
            align-items: center;
            padding-left: 30px;
            box-sizing: border-box;
            position: relative;
            i {
              height: 32px;
              display: flex;
              box-sizing: border-box;
              align-items: center;
              justify-content: center;
              position: absolute;
              width: 30px;
              top: 0;
              left: 0;
              z-index: 2;
            }
          }
        }
      }
      p.tips {
        display: block;
        font-family: "PingFang SC";
        font-style: normal;
        font-weight: normal;
        font-size: 12px;
        line-height: 18px;
        color: #939799;
        margin-top: 8px;
      }
    }
    .delImageSwiperItem {
      position: absolute;
      top: -4px;
      right: -4px;
      z-index: 2;
      display: none;
      opacity: 0.5;
    }
    &:hover {
      .delImageSwiperItem {
        display: block;
      }
    }
  }
}
</style>
